﻿package com.tvein.sanguo.swc.action.tool
{
	//flash
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.text.TextFormat;
	import fl.controls.dataGridClasses.*;
	import fl.controls.DataGrid;
	import fl.data.DataProvider;
	import fl.events.ListEvent;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.OverLay;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class ToolMission extends MovieClip
	{
		public static const NAME:String = "Tool-Mission";

		private var effectVol:int = 100;
		private var overLay:OverLay;
		
		private var missions:Object;
		private var missionList:DataGrid;
		
		public function ToolMission()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			overLay = new OverLay();			
			this.addChild(overLay);
			this.setChildIndex(overLay, 0);
			
			missionList = new DataGrid();
			missionList.x = 5;
			missionList.y = 70;
			missionList.width = 590;
			missionList.height = 310;
			this.addChild(missionList);
			
			var style:TextFormat = new TextFormat();
			style.color = 0xff6600;
			style.size = 16;
			style.font = "LiSu";
			
			missionList.setRendererStyle("textFormat", style);
			
			var c0:DataGridColumn = new DataGridColumn("ID");
			var c1:DataGridColumn = new DataGridColumn("任务");
			var c2:DataGridColumn = new DataGridColumn("说明");
				
			c0.visible = false;
			c1.width = 190;
			c2.width = 400;
			
			missionList.addColumn(c0);
			missionList.addColumn(c1);
			missionList.addColumn(c2)
			
			btnClose.addEventListener(MouseEvent.CLICK, onClick);
			btnGuide.addEventListener(MouseEvent.CLICK, onClick);
			btnOrder.addEventListener(MouseEvent.CLICK, onClick);
			btnEntrust.addEventListener(MouseEvent.CLICK, onClick);
			btnSpecial.addEventListener(MouseEvent.CLICK, onClick);
			
			mcFrame.reSize(600, 450);
			
			_resizeUI();
		}
		
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setData":
					if (params.Name == "GetRoleMission")
					{
						missions = params.Process;
						setMissionData("All");
					}
					break;
			}
		}
		
		private function setMissionData(type:String):void
		{
			var i:int;
			
			missionList.removeAll();
			missionList.invalidateList();
			
			switch(type)
			{
				case "All":
					for (i = 0; i < missions.length; i++)
					{
						missionList.addItem({"ID":missions[i].SID, "任务":missions[i].Name, "说明":missions[i].Note});
					}
					break;
				case "Guide":
					for (i = 0; i < missions.length; i++)
					{
						if (missions[i].Type == 1)
						{
							missionList.addItem({"ID":missions[i].SID, "任务":missions[i].Name, "说明":missions[i].Note});
						}
					}
					break;
				case "Order":
					for (i = 0; i < missions.length; i++)
					{
						if (missions[i].Type == 2)
						{
							missionList.addItem({"ID":missions[i].SID, "任务":missions[i].Name, "说明":missions[i].Note});
						}
					}
					break;
				case "Entrust":
					for (i = 0; i < missions.length; i++)
					{
						if (missions[i].Type == 3)
						{
							missionList.addItem({"ID":missions[i].SID, "任务":missions[i].Name, "说明":missions[i].Note});
						}
					}
					break;
				case "Special":
					for (i = 0; i < missions.length; i++)
					{
						if (missions[i].Type == 4)
						{
							missionList.addItem({"ID":missions[i].SID, "任务":missions[i].Name, "说明":missions[i].Note});
						}
					}
					break;
			}			
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			btnClose.removeEventListener(MouseEvent.CLICK, onClick);
			btnGuide.removeEventListener(MouseEvent.CLICK, onClick);
			btnOrder.removeEventListener(MouseEvent.CLICK, onClick);
			btnEntrust.removeEventListener(MouseEvent.CLICK, onClick);
			btnSpecial.removeEventListener(MouseEvent.CLICK, onClick);
		}
		
		private function _resizeUI(evt:Event = null):void
		{
			overLay.x = overLay.y = overLay.width = overLay.height = 0;
			
			this.x = (stage.stageWidth - this.width) * .5
			
			if ((stage.stageHeight - this.height) > 150)
			{
				this.y = (stage.stageHeight - this.height) * .3;
			}
			else
			{
				this.y = 45;
			}
			
			overLay.x = 0 - this.x;
			overLay.y = 0 - this.y;
			overLay.width = stage.stageWidth;
			overLay.height = stage.stageHeight;
		}

		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnClose":
					dispatchEvent(new SwcEvent(NAME, ["-close", "ToolMission"]));
					break;
				case "btnGuide":
					setMissionData("Guide");
					break;
				case "btnOrder":
					setMissionData("Order");
					break;
				case "btnEntrust":
					setMissionData("Entrust");
					break;
				case "btnSpecial":
					setMissionData("Special");
					break;
			}
		}
	}
}